<?php
header('Content-type:text/html;charset=utf-8');
date_default_timezone_set('Asia/Shanghai');
echo $date = date('Y-m-d');
$hour = intval(date('H'));


// 404日志
$body = '';
$body .= date('H:i:s')."\t";
$body .= $_SERVER['REMOTE_ADDR']."\t";
$body .= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\t";
$body .= $_SERVER['HTTP_REFERER']."\t";
$body .= $_SERVER['HTTP_USER_AGENT']."\t";
$body .= "\r\n";
file_put_contents('log_404/'.$date.'.txt', $body, FILE_APPEND);


// 备份 - 0点
$log_file = 'log_backup/'.$date;
if(!file_exists($log_file)) {
	file_get_contents('http://99juhui.com/f/?m=system&a=backup_do&type=all');
	file_get_contents('http://fetion.adwap.cn/restlet/fetion/13718462507/cakephp1/13718462507/数据已全部备份');
	touch($log_file);
	exit();
}


// 即时备份 - 每1小时
$log_file = 'log_backup_realtime/'.date('Y-m-d-H');
if(!file_exists($log_file)) {
	file_get_contents('http://99juhui.com/f/?m=system&a=backup_do&type=today');
	touch($log_file);
	exit();
}


// 待办记事 - 18点
$log_file = 'log_notes/'.$date;
if($hour > 18 && !file_exists($log_file)) {
	$res = file_get_contents('http://99juhui.com/f/?m=get&a=notes_rank2');
	file_get_contents('http://fetion.adwap.cn/restlet/fetion/13718462507/cakephp1/13718462507/'.$res);
	file_put_contents($log_file, $res);
	exit();
}


// 每天发新闻 - 8、9、12、15、18、21、0
$log_file = 'log_news/'.$date.'-'.($hour%3);
if(($hour >= 8 || $hour < 2) && !file_exists($log_file)) {
	$res = file_get_contents('http://news.baidu.com/z/hotquery/roll/');
	$res = iconv('gbk', 'utf-8', $res);
	
	preg_match('/<ul>(.*?)<\/ul>/si', $res, $words_list);
	preg_match_all('/mon=\"r=1\">(.*?)<\/a>/si', $words_list[1], $words_array);
	$text = "[{$hour}时热词]";
	foreach ($words_array[1] as $key => $item) {
		$text .= ($key + 1).'.'.$item;
	}
	$text = str_replace(' ', '_', $text);
	
	file_get_contents('http://fetion.adwap.cn/restlet/fetion/13718462507/cakephp1/13718462507/'.$text);
	file_put_contents($log_file, $text);
	exit();
}